#!/bin/bash

PLAYER=$1

test "${ACTION}" = "add" || exit 0
echo "${DEVNAME}" | grep -E "^/dev/input/event[0-9]+$" || exit 0

PARENTHASH=$(evsieve-helper parent "${DEVNAME}" input usb)
BASEFILE="/var/run/virtual-openfire-devices.${PARENTHASH}"
PIDFILE="${BASEFILE}.pid"
LOCKFILE="${BASEFILE}.lock"
LOGFILE="${BASEFILE}.log"

unlockAndExit() {
    rmdir "${LOCKFILE}"
    exit "${1}"
}

checkRunningPIDAndExit1() {
    test ! -e "${PIDFILE}" && return 0
    LPID=$(cat "${PIDFILE}")
    test ! -d "/proc/${LPID}" && return 0
    unlockAndExit 1
}

trylock() {
    # lock
    N=0
    while ! mkdir "${LOCKFILE}"
    do
	sleep 1
	let N++
	test "${N}" -gt 30 && exit 1 # give up
    done
}

trylock
checkRunningPIDAndExit1

CHILDREN=$(evsieve-helper children "${PARENTHASH}" input usb | grep -vE "OpenFIRE FIRECon P[1-4]$") # remove the joystick from the event cause it duplicates events
NDEVS=$(echo "${CHILDREN}" | wc -l)

    ############
    # Based on the enclosed instruction book from official Github
    ############
    # Trigger                           : BTN_LEFT
    # A / PUMP                          : BTN_RIGHT
    # B                                 : BTN_MIDDLE
    # Foot pedal / C                    : BTN_SIDE
    # Alt pedal                         : BTN_EXTRA
    # Start                             : KEY_1
    # Select                            : KEY_5
    # D-pad up                          : KEY_UP
    # D-pad down                        : KEY_DOWN
    # D-pad left                        : KEY_LEFT
    # D-pad right                       : KEY_RIGHT
    ############
    # Player 1 light gun
    ############
    # Start                             : KEY_1
    # Select                            : KEY_5
    ############
    # Player 2 light gun
    ############
    # Start                             : KEY_2
    # Select                            : KEY_6
    ############
    # Player 3 light gun
    ############
    # Start                             : KEY_3
    # Select                            : KEY_7
    ############
    # Player 4 light gun
    ############
    # Start                             : KEY_4
    # Select                            : KEY_8

if test "${NDEVS}" = 2
then
    DEV1=$(echo "${CHILDREN}" | head -1           | cut -f 1)
    DEV2=$(echo "${CHILDREN}" | head -2 | tail -1 | cut -f 1)

    if test "${PLAYER}" = "1"
    then
	nohup evsieve                                        \
                --input "${DEV1}" "${DEV2}" persist=exit \
                --map yield btn:middle     btn:2         \
                --map yield btn:side       btn:right     \
                --map yield key:1          btn:middle    \
                --map yield key:5          btn:1         \
                --map yield key:up         btn:5         \
                --map yield key:down       btn:6         \
                --map yield key:left       btn:7         \
                --map yield key:right      btn:8         \
                --block btn:extra                        \
                --output name="OpenFIRE light gun P1" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "2"
    then
	nohup evsieve                                        \
                --input "${DEV1}" "${DEV2}" persist=exit \
                --map yield btn:middle     btn:2         \
                --map yield btn:side       btn:right     \
                --map yield key:2          btn:middle    \
                --map yield key:6          btn:1         \
                --map yield key:up         btn:5         \
                --map yield key:down       btn:6         \
                --map yield key:left       btn:7         \
                --map yield key:right      btn:8         \
                --block btn:extra                        \
                --output name="OpenFIRE light gun P2" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "3"
    then
	nohup evsieve                                        \
                --input "${DEV1}" "${DEV2}" persist=exit \
                --map yield btn:middle     btn:2         \
                --map yield btn:side       btn:right     \
                --map yield key:3          btn:middle    \
                --map yield key:7          btn:1         \
                --map yield key:up         btn:5         \
                --map yield key:down       btn:6         \
                --map yield key:left       btn:7         \
                --map yield key:right      btn:8         \
                --block btn:extra                        \
                --output name="OpenFIRE light gun P3" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "4"
    then
	nohup evsieve                                        \
                --input "${DEV1}" "${DEV2}" persist=exit \
                --map yield btn:middle     btn:2         \
                --map yield btn:side       btn:right     \
                --map yield key:4          btn:middle    \
                --map yield key:8          btn:1         \
                --map yield key:up         btn:5         \
                --map yield key:down       btn:6         \
                --map yield key:left       btn:7         \
                --map yield key:right      btn:8         \
                --block btn:extra                        \
                --output name="OpenFIRE light gun P4" >/dev/null 2>"${LOGFILE}" &
    fi

    echo $! > "${PIDFILE}"
else
    unlockAndExit 1
fi

unlockAndExit 0
